#import "SearchViewController.h"

@implementation SearchViewController

/*
 // Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically.
 - (void)loadView {
 }
 */

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
	bookmarkData = [[BookmarksDataSource alloc] initWithDelegate:self];
	[[bookmarkData bookmarks] addObject:@"BlaBla"];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientation
    return (YES);//interfaceOrientation == (UIInterfaceOrientationPortrait));
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar
{
	// only show the status bar's cancel button while in edit mode
	searchBar.showsCancelButton = YES;
	[addBookmarkButton setHidden:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
{
	searchBar.showsCancelButton = NO;
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:
							 [NSURL URLWithString: [NSString stringWithFormat:@"http://cohens.co.il/server.php?line=%@", searchBar.text]]];
	
	NSURLConnection *connection = [[NSURLConnection alloc] 
								   initWithRequest:request 
								   delegate:self]; 
	[connection release]; 
	[request release]; 
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	[messageView setBackgroundColor:[UIColor clearColor]];
	[timeTableView setTableHeaderView:messageView];
	[searchBar resignFirstResponder];	
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
	NSLog (@"connectionDidReceiveData"); 
	NSString *newText = [[NSString alloc] 
						 initWithData:data 
						 encoding:NSUTF8StringEncoding]; 
	if (newText != NULL) { 
		NSArray* timeData = [newText componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"]];
		searchData = [[BusRouteDataSource alloc] initFromPlace:(NSString*)[timeData objectAtIndex:0]
													   toPlace:(NSString*)[timeData objectAtIndex:1]
												 withHoursFrom:(NSString*)[timeData objectAtIndex:3]
													andHoursTo:(NSString*)[timeData objectAtIndex:4]];
		[searchBar retain];
		
		[timeTableView setDataSource:searchData];
		[timeTableView setTableHeaderView:nil];
		[timeTableView reloadData];
		
		[addBookmarkButton setHidden:NO];
		
		[newText release]; 
	} 
} 

- (void) connectionDidFinishLoading: (NSURLConnection*) connection { 
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 


- (void)searchBarCancelButtonClicked:(UISearchBar *)bar
{
	[searchBar resignFirstResponder];
}

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
	//bookmarkData = [[BookmarksDataSource alloc] initWithDelegate:self];
	[timeTableView setDataSource:bookmarkData];
	[timeTableView setTableHeaderView:nil];
	[timeTableView reloadData];
}

- (void) segmentedClick: (id)sender
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[UIView beginAnimations:nil context:context]; 
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
	[UIView setAnimationDuration:0.3]; 
	CGRect moveby = searchBar.frame;
	
	switch([sender selectedSegmentIndex] + 1) 
	{ 
		case 1: moveby.origin.y += searchBar.bounds.size.height; break; 
		case 2: moveby.origin.y = 0; break; 
		default: break; 
	}

	
	[searchBar setFrame:moveby];
	
	[UIView commitAnimations];
	
	NSLog(@"%i", [sender selectedSegmentIndex]+1);
}
@end
